package de.koller.planet2D.frame;

import de.koller.oceanFramework.tree.Child;
import de.koller.planet2D.frame.previewRender.RenderPanel;
import de.koller.planet2D.PNode;
import de.koller.planet2D.container.RenderObj;
import de.koller.planet2D.frame.control.EditorControl;
import de.koller.planet2D.frame.previewRender.PRenderRoot;
import de.koller.planet2D.frame.rendering.Renderizer;
import de.koller.planet2D.rcom.AddPNode;

public class PreviewFrame extends PNode {

	public int time = 0;
	public int maxTime = 0;
	public final PreviewFrameComp comp = new PreviewFrameComp( this );
	@Child public final PRenderRoot pRenderNode = new PRenderRoot();
	@Child public final EditorControl editorControl = new EditorControl();
	public final RenderPanel renderPanel = new RenderPanel( pRenderNode );
	
	private ObjectFrame currentObjFrame = null;
	
	@Override
	protected void init() {
		super.init();
		
		comp.add( renderPanel );
		comp.pack();
		comp.setVisible( true );
	}

	@Override
	protected void render( int elapsedTime ) {
		super.render( elapsedTime );
		
		renderPanel.repaint();
	}
	
	public final void openObjectEdit( RenderObj obj ) {
		if( currentObjFrame != null )
			return;
		
		if( obj == null ) {
			proot.call( new AddPNode( this, new ObjectFrame() ) );
		} else
			proot.call( new AddPNode( this, new ObjectFrame( obj ) ) );
		
		proot.updateThis();
	}
	
	public final void openNewObject() {
		openObjectEdit( null );
	}
	
	public final void openRenderer() {
		proot.call( new AddPNode( this, new Renderizer() ) );
	}
	
	public final void closeThis() {
		this.comp.dispose();
		this.proot.exit();
	}

	public void setCurrent( ObjectFrame current ) {
		this.currentObjFrame = current;
	}

	public ObjectFrame getCurrentObjFrame() {
		return currentObjFrame;
	}
	
}
